package com.mcp_life.module.servicemodules.service.Impl;

import com.mcp_life.common.context.UserContextHolder;
import com.mcp_life.common.exception.BusinessException;
import com.mcp_life.entity.dto.SupermaketProductDTO;
import com.mcp_life.entity.pojo.SupermarketProduct;
import com.mcp_life.entity.pojo.SupermarketStore;
import com.mcp_life.module.servicemodules.mapper.SupermarketMapper;
import com.mcp_life.module.servicemodules.service.SupermarketService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;

@Service
@Slf4j
public class SupermarketServiceImpl implements SupermarketService {
    @Autowired
    private SupermarketMapper supermarketMapper;
    @Override
    public void addProduct(SupermaketProductDTO supermarketProductDTO) {
        //根据当前登录的userid，查询对应的店铺id
        String userId= UserContextHolder.getUserId();
        SupermarketProduct supermarketProduct=new SupermarketProduct();
        BeanUtils.copyProperties(supermarketProductDTO,supermarketProduct);
        SupermarketStore supermarketStore=supermarketMapper.selectByUserId(userId);
        if (supermarketStore!=null){
           supermarketProduct.setStoreId(supermarketStore.getId());
           supermarketProduct.setCreatedTime(new Timestamp(System.currentTimeMillis()));
           supermarketProduct.setUpdateTime(new Timestamp(System.currentTimeMillis()));
           supermarketProduct.setStatus(1);//默认上架
            supermarketMapper.addProduct(supermarketProduct);
            log.info("添加商品成功{}",supermarketProduct);
        }
        else {
            throw new BusinessException(750,"未找到该用户的超市店铺");
        }
    }

    /**
     * 展示所有的开店的店铺
     */

}
